package logic

import (
	"context"
	"github.com/pkg/errors"
	"mall/app/product/cmd/rpc/internal/svc"
	"mall/app/product/cmd/rpc/pb"
	"mall/app/product/model"
	"mall/common/xerr"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateCategoryLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateCategoryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateCategoryLogic {
	return &UpdateCategoryLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// UpdateCategory 修改分类
func (l *UpdateCategoryLogic) UpdateCategory(in *pb.CategoryEntity) (*pb.EmptyResp, error) {
	err := l.svcCtx.CategoryModel.Update(l.ctx, &model.PmsCategory{
		CatId:        in.CatId,
		Name:         in.Name,
		ParentCid:    in.ParentCid,
		CatLevel:     in.CatLevel,
		ShowStatus:   in.ShowStatus,
		Sort:         in.Sort,
		Icon:         in.Icon,
		ProductUnit:  in.ProductUnit,
		ProductCount: in.ProductCount,
	})
	if err != nil {
		return nil, errors.Wrapf(xerr.NewErrorWithCode(xerr.DbError), "CategoryModel.Update err: %v", err)
	}
	return &pb.EmptyResp{}, nil
}
